package com.example.highclasscomponent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.highclasscomponent.R;

import java.util.ArrayList;
import java.util.List;

public class LaunchPageAdapter extends PagerAdapter {
    private Context context;
    int []imageList;
    List<View>  pageView=new ArrayList<>();

    public LaunchPageAdapter(Context context, int[] imageList) {
        this.context = context;
        this.imageList = imageList;

        for (int i = 0; i < imageList.length; i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_launch, null);
            ImageView iv_launch = view.findViewById(R.id.iv_launch);
            RadioGroup rg_indicate=view.findViewById(R.id.rg_indicate);
            Button btn_start = view.findViewById(R.id.btn_start);
            iv_launch.setImageResource(imageList[i]);
            pageView.add(view);

            //每个页面都有一组对应的单选按钮（并不复用）
            for (int j = 0; j < imageList.length; j++) {
                RadioButton radio = new RadioButton(context);
                radio.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                radio.setPadding(10,10,10,10);
                rg_indicate.addView(radio);
            }
            ((RadioButton)rg_indicate.getChildAt(i)).setChecked(true);
            if(i==imageList.length-1)
            {
                btn_start.setVisibility(View.VISIBLE);
                btn_start.setOnClickListener(v->{
                    Toast.makeText(context, "立刻开始美好生活", Toast.LENGTH_SHORT).show();
                });
            }
        }
    }

    @Override
    public int getCount() {
        return imageList.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view=pageView.get(position);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(pageView.get(position));
    }
}
